// stores/contractHistory.js
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useContractHistoryStore = defineStore('contractHistory', () => {
  // 使用ref确保响应式
  const historyRecords = ref([])

  // 添加变更记录
  const addRecord = (contract, actionType) => {
    const record = {
      id: Date.now(),
      contractNumber: contract.number,
      action: actionType, // 'create' 或 'update'
      data: JSON.parse(JSON.stringify(contract)), // 深拷贝当前数据
      timestamp: new Date().toISOString()
    }
    historyRecords.value.unshift(record) // 新记录放在最前面
  }

  // 获取特定合同的历史记录
  const getContractHistory = (contractNumber) => {
    return historyRecords.value.filter(r => r.contractNumber === contractNumber)
  }

  return { 
    historyRecords,
    addRecord,
    getContractHistory
  }
})